package com.lagou.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.domain.Goods;
import com.lagou.goods.mapper.GoodsMapper;
import com.lagou.goods.service.GoodsService;
import com.lagou.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 根据id获取商品信息
     * @param id
     * @return
     */
    @Override
    public Goods getGoodsById(Integer id) {
        return goodsMapper.selectById(id);
    }

    /**
     * 根据id删除商品信息
     * @param id
     */
    @Override
    public void deleteGoodsById(Integer id) {
        goodsMapper.deleteById(id);
    }

    /**
     * 更新商品信息
     * @param goods
     */
    @Override
    public void updateGoodsById(Goods goods) {
        goodsMapper.updateById(goods);
    }

    /**
     * 分页获取商品信息
     * @param goodsVO
     * @return
     */
    @Override
    public Map<String, Object> getGoodsByPage(GoodsVO goodsVO) {
        System.out.println(goodsVO);
        PageHelper.startPage(goodsVO.getStartPage(), goodsVO.getPageSize());
        List<Goods> goodsList = goodsMapper.getGoodsListByPage(goodsVO);
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        Map<String,Object> map = new HashMap<>();
        map.put("list",goodsList);
        map.put("total",pageInfo.getTotal());
        return map;
    }

    /**
     * 根据id获取商品信息
     * @param ids
     * @return
     */
    @Override
    public List<Goods> getGoodsByIds(List<Integer> ids) {
        return goodsMapper.getGoodsByIds(ids);
    }
}
